package com.luxsoft.siipap.em.replica.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;

import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

import org.jdesktop.swingx.JXTable;

import ca.odell.glazedlists.gui.TableFormat;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.luxsoft.siipap.swing.browser.FilteredBrowserPanel;

/**
 * Panel para mostrar las existencias en ambos sistemas
 * 
 * @author Ruben Cancino
 *
 */
public class ExistenciasPanel extends FilteredBrowserPanel<Map> implements ActionListener{
	
	private final ExistenciasModel model;
	public JTextField claveField=new JTextField(10);
	
	public ExistenciasPanel(final ExistenciasModel model){
		super(Map.class);
		this.model=model;
		claveField.addActionListener(this);
	}
	
	@Override
	protected JComponent buildContent() {		
		JPanel panel=new JPanel(new BorderLayout());
		JSplitPane sp=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		sp.setResizeWeight(.5);
		sp.setOneTouchExpandable(true);
		sp.setTopComponent(buildExistenciasDBF());
		panel.add(sp,BorderLayout.CENTER);
		return panel;		
		
	}
		


	protected JXTable buildGrid(){
		grid=new JXTable(10,50);
		return grid;
	}

	private JComponent buildExistenciasDBF(){
		JPanel panel=new JPanel(new BorderLayout());
		panel.add(buildGrid(),BorderLayout.CENTER);
		return panel;
		
	}
	

	protected TableFormat buildTableFormat() {
		DBFTableFormat tf=new DBFTableFormat();
		tf.setCampos("SASUCURSAL","SAARTICULO","SASALDINI","SASALDACT","SASALANTAJ","SASALDESAJ","SAULTOPER"
				    ,"SAFECHAJU","SASALDEXTE","SAFECHEXTE","SAINIDETAJ","SATOTPEDPE","SAULTPEDEL","SAULTENTPE"
				    ,"SATOTPEDCL","SAULTVENTA","SAACUVENTA","SAACUIMPVT","SAACUDEVVE","SAACUIMPDV","SAMOVXATE"
				    ,"SAUNIENCOR","SAMODENCOR");
		return tf;
	}

	@Override
	protected List<Map> findData() {
		return model.busacarExistenciasDBF(claveField.getText());
	}

	@Override
	protected void installCustomComponentsInFilterPanel(DefaultFormBuilder builder){
		builder.append("Clave",claveField);
	}

	@Override
	public Action[] getActions() {
		return new Action[]{getLoadAction()};
	}

	public void actionPerformed(ActionEvent e) {
		load();
		
	}

	
	
	
}
